package io.renren.modules.app.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.app.dao.company.YcCollectCompanyDao;
import io.renren.modules.app.dao.index.YcRecruitDao;
import io.renren.modules.app.dao.index.YcSpreadDao;
import io.renren.modules.app.dao.index.YcSpreadRecordDao;
import io.renren.modules.app.dao.info.YcUserInterviewDao;
import io.renren.modules.app.dao.info.YcUserPostMapper;
import io.renren.modules.app.dao.user.UserResumeDao;
import io.renren.modules.app.entity.company.YcCollectCompany;
import io.renren.modules.app.entity.index.YcSpreadEntity;
import io.renren.modules.app.entity.index.YcSpreadRecord;
import io.renren.modules.app.entity.info.CompanyRecuit;
import io.renren.modules.app.entity.info.YcUserInterview;
import io.renren.modules.app.entity.info.YcUserPost;
import io.renren.modules.app.entity.user.UserResumeEntity;
import io.renren.modules.app.service.user.UserResumeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.common.utils.PageUtils;



@Service("UserResumeService")
@RequiredArgsConstructor
public class UserResumeServiceImpl extends ServiceImpl<UserResumeDao, UserResumeEntity> implements UserResumeService {

    private final UserResumeDao userResumeDao;
    private final YcUserPostMapper userPostMapper;
    private final YcRecruitDao ycRecruitDao;
    private final YcCollectCompanyDao ycCollectCompanyDao;
    private final YcUserInterviewDao userInterviewDao;
    private final YcSpreadDao ycSpreadDao;
    private final YcSpreadRecordDao ycSpreadRecordDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }

    @Override
    public UserResumeEntity selectByUserId(Integer userId) {
        LambdaQueryWrapper<UserResumeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserResumeEntity::getUserId, userId);
        wrapper.last(" limit 1 ");
        return this.getOne(wrapper);
    }

    @Override
    public UserResumeEntity selectOneByUserId(Integer userId) {
        return userResumeDao.selectById(userId);
    }

    @Override
    public List<UserResumeEntity> selectListByUserId(Integer userId) {
        List<UserResumeEntity> list = new ArrayList<>();
        list.add(selectByUserId(userId));
        return list;
    }

    @Override
    public boolean doDeliverVita(Integer userId, Integer recruitId) {
        if (Objects.nonNull(userId) && Objects.nonNull(recruitId)) {
            YcUserPost userPost = new YcUserPost();
            userPost.setAddtime(new Date());
            userPost.setDeletestatus(false);
            userPost.setResumeId(userId);
            userPost.setRecruitId(recruitId);
            userPost.setPostStatus(0);
            return userPostMapper.insert(userPost) > 0;
        }
        return false;
    }

    @Override
    public boolean checkDeliverVita(Integer userId, Integer recruitId) {
        if (Objects.nonNull(userId) && Objects.nonNull(recruitId)) {
            LambdaQueryWrapper<YcUserPost> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YcUserPost::getResumeId, userId);
            queryWrapper.eq(YcUserPost::getRecruitId, recruitId);
            return userPostMapper.selectList(queryWrapper).size() > 0;
        }
        return false;
    }

    @Override
    public List<YcUserPost> selectUserPostByUserId(Integer userId) {
        List<YcUserPost> postList = new ArrayList<>();
        if (Objects.nonNull(userId)) {
            postList = ycRecruitDao.selectUserPostData(userId);
        }
        return postList;
    }

    @Override
    public List<YcUserPost> selectCompanyPostById(Integer companyId) {
        List<YcUserPost> postList = new ArrayList<>();
        if (Objects.nonNull(companyId)) {
            postList = ycRecruitDao.selectCompanyPostData(companyId);
        }
        return postList;
    }

    @Override
    public boolean updateCompanyPostStatus(Integer postId) {
        return userPostMapper.updateById(new YcUserPost().setPostStatus(1).setId(postId)) > 0;
    }

    @Override
    public boolean saveCollectCompany(YcCollectCompany company){
        if (exitsCollectCompany(company.getCompanyId(), company.getResumeId())) {
            return true;
        }
        company.setAddtime(new Date());
        company.setDeletestatus(false);
        return ycCollectCompanyDao.insert(company) > 0;
    }

    @Override
    public boolean deleteCollectCompany(Integer companyId, Integer resumeId){
        LambdaQueryWrapper<YcCollectCompany> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YcCollectCompany::getCompanyId, companyId);
        queryWrapper.eq(YcCollectCompany::getResumeId, resumeId);
        return ycCollectCompanyDao.delete(queryWrapper) > 0;
    }

    @Override
    public List<YcCollectCompany> queryCollectCompany(Integer companyId, Integer resumeId){
        List<YcCollectCompany> collectCompanies = new ArrayList<>();
        if (Objects.nonNull(companyId) && Objects.nonNull(resumeId)) {
            LambdaQueryWrapper<YcCollectCompany> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YcCollectCompany::getCompanyId, companyId);
            queryWrapper.eq(YcCollectCompany::getResumeId, resumeId);
            collectCompanies = ycCollectCompanyDao.selectList(queryWrapper);
        }
        return collectCompanies;
    }

    @Override
    public List<YcCollectCompany> queryCollectCompanyByCompanyId(Integer companyId){
        List<YcCollectCompany> collectCompanies = new ArrayList<>();
        if (Objects.nonNull(companyId)) {
            LambdaQueryWrapper<YcCollectCompany> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YcCollectCompany::getCompanyId, companyId);
            collectCompanies = ycCollectCompanyDao.selectList(queryWrapper);
        }
        return collectCompanies;
    }

    @Override
    public boolean exitsCollectCompany(Integer companyId, Integer resumeId){
        return queryCollectCompany(companyId, resumeId).size() > 0;
    }

    @Override
    public List<YcCollectCompany> selectCompanyCollectList(Integer companyId) {
        List<YcCollectCompany> collectCompanies = new ArrayList<>();
        if (Objects.nonNull(companyId)) {
            collectCompanies = ycRecruitDao.selectCompanyCollectList(companyId);
        }
        return collectCompanies;
    }

    @Override
    public List<CompanyRecuit> selectCompanyRecuitList(Integer companyId) {
        List<CompanyRecuit> recuits = new ArrayList<>();
        if (Objects.nonNull(companyId)) {
            recuits = ycRecruitDao.selectCompanyRecuitList(companyId);
        }
        return recuits;
    }

    @Override
    public boolean saveUserInterview(YcUserInterview userInterview) {
        userInterview.setAddtime(new Date());
        String dateString = userInterview.getTimes() + " 00:00:00";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = dateFormat.parse(dateString);
            userInterview.setInterviewTime(date);
            userInterview.setDeletestatus(false);
            return userInterviewDao.insert(userInterview) > 0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<YcUserInterview> selectCompanyInterList(Integer companyId, Integer resumeId, Integer recrutId) {
        List<YcUserInterview> interviews = new ArrayList<>();
        if (Objects.nonNull(companyId) && Objects.nonNull(resumeId) && Objects.nonNull(recrutId)) {
            interviews = ycRecruitDao.selectCompanyInterList(companyId, resumeId, recrutId);
        }
        return interviews;
    }

    @Override
    public List<YcUserInterview> selectCompanyInterList(Integer companyId, String status) {
        List<YcUserInterview> interviews = new ArrayList<>();
        if (Objects.nonNull(companyId)) {
            interviews = ycRecruitDao.selectInterByCompanyList(companyId, status);
        }
        return interviews;
    }

    @Override
    public List<YcUserInterview> selectUserInterList(Integer userId, String status) {
        List<YcUserInterview> interviews = new ArrayList<>();
        if (Objects.nonNull(userId)) {
            interviews = ycRecruitDao.selectInterByUserList(userId, status);
        }
        return interviews;
    }

    @Override
    public boolean updateInterStatus(Integer id, String status) {
        if (Objects.nonNull(id) && StringUtils.isNotEmpty(status)) {
            return userInterviewDao.updateById(new YcUserInterview().setId(id)
                    .setInterviewStatus(Integer.parseInt(status))) > 0;
        }
        return false;
    }

    @Override
    public Integer getConsumePoints() {
        LambdaQueryWrapper<YcSpreadEntity> queryWrapper = new LambdaQueryWrapper<YcSpreadEntity>();
        queryWrapper.eq(YcSpreadEntity::getDeletestatus, false);
        List<YcSpreadEntity> spreadEntities = ycSpreadDao.selectList(queryWrapper);
        if (spreadEntities.size() > 0) {
            return spreadEntities.get(0).getConsumePoints();
        }
        return 0;
    }

    public boolean checkSpreadStatus (Integer companyId) {
        List<YcSpreadRecord> spreadRecords =
                ycRecruitDao.selectSpreadRecordAllByCompanyId(companyId);
        if (spreadRecords.size() > 0) {
            return true; // 在时间内
        } else {
            return false; // 不在时间内
        }
    }

    @Override
    public boolean saveSpreadRecord(Integer companyId) {
        if (Objects.nonNull(companyId)) {
            //检测是否已经开启
            List<YcSpreadRecord> spreadRecords =
                    ycRecruitDao.selectSpreadRecordAllByCompanyId(companyId);
            if (spreadRecords.size() > 0) {
                ycSpreadRecordDao.updateById(spreadRecords.get(0).setDeletestatus(false));
                return true;
            } else {
                ycSpreadRecordDao.delete(new LambdaQueryWrapper<YcSpreadRecord>()
                        .eq(YcSpreadRecord::getCompanyId, companyId));
                YcSpreadRecord ycSpreadRecord = new YcSpreadRecord();
                ycSpreadRecord.setAddtime(new Date());
                ycSpreadRecord.setDeletestatus(false);
                ycSpreadRecord.setBeginTime(new Date());
                ycSpreadRecord.setAccessNum(0);
                ycSpreadRecord.setCompanyId(companyId);
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.MONTH, 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(calendar.getTime());
                try {
                    Date date = sdf.parse(formattedDate);
                    ycSpreadRecord.setEndTime(date);
                    ycSpreadRecordDao.insert(ycSpreadRecord);
                    // 扣去积分
                    Integer fen = getConsumePoints();
                    ycRecruitDao.updateCompanySpreadFen(companyId, fen);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 更新推广状态：status：1开启，2关闭
     */
    @Override
    public boolean updateStatusSpreadRecord(Integer companyId, Integer status) {
        if (Objects.nonNull(companyId) && Objects.nonNull(status)) {
            YcSpreadRecord ycSpreadRecord = new YcSpreadRecord();
            ycSpreadRecord.setCompanyId(companyId);
            if (status == 1) {
                ycSpreadRecord.setDeletestatus(false);
            } else if (status == 2) {
                ycSpreadRecord.setDeletestatus(true);
            }
            return ycSpreadRecordDao.update(null, new LambdaUpdateWrapper<YcSpreadRecord>()
                    .set(YcSpreadRecord::getDeletestatus, ycSpreadRecord.getDeletestatus())
                    .eq(YcSpreadRecord::getCompanyId, ycSpreadRecord.getCompanyId())) > 0;
        }
        return false;
    }

    @Override
    public boolean exitsSpreadRecord(Integer companyId) {
        if (Objects.nonNull(companyId)) {
            List<YcSpreadRecord> spreadRecords =
                    ycRecruitDao.selectSpreadRecordListByCompanyId(companyId);
            if (spreadRecords.size() > 0) {
                return true;
            }
        }
        return false;
    }
}